package com.msb.mall.coupon.service.impl;

import com.msb.mall.common.dto.SkuReductionDTO;
import com.msb.mall.coupon.entity.MemberPriceEntity;
import com.msb.mall.coupon.entity.SkuLadderEntity;
import com.msb.mall.coupon.service.MemberPriceService;
import com.msb.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.mall.common.utils.PageUtils;
import com.msb.mall.common.utils.Query;

import com.msb.mall.coupon.dao.SkuFullReductionDao;
import com.msb.mall.coupon.entity.SkuFullReductionEntity;
import com.msb.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void savaSkuReduction(SkuReductionDTO skuReductionDTO) {
        // 5.3 保存满减信息，满折信息，会员价格信息，sku的优惠信息 pms_sku_ladder sms_sku_full_reduction sms_sku_ladder_price
        // 5.3.1 保存满减信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionDTO.getSkuId());
        skuLadderEntity.setFullCount(skuReductionDTO.getFullCount());
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        // 5.3.2 保存满折信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionDTO, skuFullReductionEntity);
        this.save(skuFullReductionEntity);
        // 5.3.3 保存会员价格信息
        if (skuReductionDTO.getMemberPrice()!= null && skuReductionDTO.getMemberPrice().size() > 0) {
            List<MemberPriceEntity> memberPriceEntities = skuReductionDTO.getMemberPrice().stream().map(memPrice -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionDTO.getSkuId());
                memberPriceEntity.setMemberLevelId(memPrice.getId());
                memberPriceEntity.setMemberPrice(memPrice.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

}